import { ref, onMounted, reactive, nextTick } from 'vue'
import router from '@/router/index'
import { useRoute } from 'vue-router'
import { addAddress } from '../api/api'
import { showToast } from 'vant';

//添加地址数据的hooks

export function useAddress() {
    const route = useRoute()
    //高亮显示
    const flag = ref(0)
    
    //地址数据
    //button数据显示逻辑
    const addrList = ref(['公司', '家', '学校'])
    const clickCpn = (item, index) => {
        flag.value = index
        ruleForm.Labelress = item
        
    }

  

    //传递的地址
    const ruleForm = reactive({
        id:11,
        name: '',
        sex: '男',
        tel: '',
        address: route.query.Houseaddress,
        addressDetail: '',
        isDefault: false,
        Labelress: '公司'

    })

    //正则判断
    const namerules = reactive(
        [{ required: true, message: '不能为空' },
        { validator: (val) => /^[\u4100-\u9fa5]{2,5}$/.test(val), message: '请输入2-5位中文', trigger: 'onBlur' }]
    )

    const telrules = reactive(
        [{ required: true, message: '不能为空' },
        { validator: (val) => /1\d{10}/.test(val), message: '请输入正确11位数 1开头', trigger: 'onBlur' }]
    )



    //获取地址 跳转到地址
    const onSubmit =async () => {
        
        // console.log('submit', values);
        let res = await addAddress(ruleForm)
        
        if(res.status == 200){
            showToast('添加成功');
            setTimeout(() => {
                router.push('/providehome')
            }, 1000);       
        }else{
            showToast('添加失败，请输入正确的数据');
        }
        
    
        
       
    };



    //跳转
    const Addaddress = () => {
        router.push('/addressList')
    }





    return {
        clickCpn, addrList,
        ruleForm, namerules, telrules, onSubmit, Addaddress, flag
    }
}












